package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelCommentReply;
import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.Travelcontent;
import cn.wolfcode.trip.base.mapper.SystemMessageMapper;
import cn.wolfcode.trip.base.mapper.TravelCommentReplyMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.TravelcontentMapper;
import cn.wolfcode.trip.base.query.TravelObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
     private TravelcontentMapper travelcontentMapper;
    @Autowired
     private SystemMessageMapper systemMessageMapper;
    @Autowired
     private TravelCommentReplyMapper commentReplyMapper;


    @Override
    public PageInfo<?> query(TravelObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list=travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Travel> listTravelByStrategyId(Long id) {
        return travelMapper.listTravelByStrategyId(id);
    }



    //查询热门
    @Override
    public PageInfo<?> queryByState(TravelObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list=travelMapper.selectByState(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Travel travel) {
        Date date=new Date();
        travel.setCreateTime(date);
        travel.setLastUpdateTime(date);
        travelMapper.insert(travel);
        Travelcontent travelcontent = travel.getTravelcontent();
        travelcontent.setId(travel.getId());
        travelcontentMapper.insert(travelcontent);

    }

    @Override
    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Travel travel) {
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);
        Travelcontent travelcontent = travel.getTravelcontent();
        travelcontent.setId(travel.getId());
        travelcontentMapper.updateByPrimaryKey(travelcontent);
    }

    @Override
    public Travelcontent getContentById(Long id) {
        return    travelcontentMapper.selectByPrimaryKey(id);

    }

    @Override
    public void changeStateById(Travel travel) {
        Travel temp = travelMapper.selectByPrimaryKey(travel.getId());
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setSentTime(new Date());
        systemMessage.setTravel(travel);
        systemMessage.setUserId(temp.getAuthor().getId());
        if(travel.getState()== Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
            systemMessage.setStateText("游记发布成功!");
        systemMessageMapper.insert(systemMessage);
        }else if(travel.getState()== Travel.STATE_REJECT){
            systemMessage.setStateText("游记被拒绝发布!,请保持社区健康环境!");
        systemMessageMapper.insert(systemMessage);
        }
        travelMapper.changeStateById(travel);
    }

    public void saveReply(TravelCommentReply commentReply) {
        commentReply.setCreateTime(new Date());
        commentReplyMapper.insert(commentReply);
    }
}
